package server.gui;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import server.LogException;


/**
 * <h1>Class GraphicLineChart</h1>
 * 
 * This class contains a set of points to display a line chart. It can
 * contain several categories to display them in the same chart.
 * 
 * @author Tomas
 * @date March 11 2009
 *
 */
public class GraphicLineChart {

	protected DefaultCategoryDataset dataset = null;
	
	protected String[] categories;
	protected String title = null;
	protected String XLabel = null;
	protected String YLabel = null;
	protected long numPoints = 0;
	protected int totalPoints;
	
	public GraphicLineChart(String[] cat, int points, String title, String XAxisLabel, String YAxisLabel){
		
		dataset = new DefaultCategoryDataset();
		
		this.title = title;
		categories = cat;
		XLabel = XAxisLabel;
		YLabel = YAxisLabel;
		this.numPoints = 0;
		totalPoints = points;
		
		for(int i = 0; i < categories.length; i++){
			for(int j = 0; j < points; j++){
				dataset.setValue(0.0, categories[i] , "" + j);
				if(i == 0){
					this.numPoints++;
				}
			}
		}
		
	}
	
	public void addPoint(int cat, double value){
		
		dataset.setValue(value, categories[cat], "" + numPoints);
		try{
			if(cat == 0){
				dataset.removeColumn("" + (numPoints-totalPoints));
				numPoints++;
			}
		}catch(Exception e){
			LogException.getInstance().logException(e);
		}
		
		
	}
	
	public ImageIcon getChart(int width, int height){
		
		JFreeChart chart = ChartFactory.createLineChart(title, XLabel, YLabel, dataset, PlotOrientation.VERTICAL, true, false, false);
		BufferedImage image = chart.createBufferedImage(width, height);
		
		return new ImageIcon(image);
		
	}
}
